package com.example.zz.example.H5;

import android.app.Person;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zz.example.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class H5AndroidActivity extends AppCompatActivity {

    private WebView webview;
    private TextView tvJs;
    private TextView tvJsArgs;
    private TextView tvShowMsg;
    private TextView tvJsCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_android);

        setWebview();
        initView();
    }

    private void initView() {
        tvJs = (TextView) findViewById(R.id.tv_androidcalljs);
        tvJsArgs = (TextView) findViewById(R.id.tv_androidcalljsargs);
        tvJsCallback = (TextView) findViewById(R.id.tv_androidcalljscallback);
        tvShowMsg = (TextView) findViewById(R.id.tv_showmsg);

        tvJs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webview.loadUrl("javascript:javacalljs()");
            }
        });

        tvJsArgs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webview.loadUrl("javascript:javacalljswith(" + "'params 3.14'" + ")");
            }
        });

        tvJsCallback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("functionName", "getTitle");
                    jsonObject.put("parameter", "");
                    jsonObject.put("successCallback", "getTitleSuccess");
                    jsonObject.put("failCallback", "getTitleFail");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                webview.loadUrl("javascript:javacalljswithcallback(" + jsonObject.toString() + ")");
            }
        });
    }

    private void setWebview() {
        webview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webview.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        //与js交互必须设置
        webSettings.setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/html.html");
        //本类提供回调方法
        webview.addJavascriptInterface(H5AndroidActivity.this, "android");
        //其它类提供方法，同样的TyyCallBack，可以取不同的名字，通过不同的名字在html中调用也是可以的
        webview.addJavascriptInterface(new TyyCallBack(), "getParams");
        webview.addJavascriptInterface(new TyyCallBack(), "androidTyyCallback");
    }

    @JavascriptInterface
    public void jsCallAndroid() {
        tvShowMsg.setText("Js invoke Android method");
    }

    @JavascriptInterface
    public void jsCallAndroidArgs(String args) {
        tvShowMsg.setText(args);
    }
    @JavascriptInterface
    public void getTitleSuccess(String args) {
        tvShowMsg.setText(args);
    }
    @JavascriptInterface
    public void getTitleFail(String args) {
        tvShowMsg.setText(args);
    }

    public class TyyCallBack {
        @JavascriptInterface
        public void taiYYNative(final String json) {
            Log.e("eeeeee", "tyyNativeBus: " + json);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("eeeeee", "tyyNativeBus: " + json);
//                    tyyNativeBus: {"functionName":"getUserInfo","parameter":"","callback":"NSS_S1667478006239_719512_7","fail":"NSS_F1667478006239_719512_7"}
//                    通过functionName传过来的名称不同扩展不同的方法调用
                    if (!TextUtils.isEmpty(json)) {
                        try {

                            JSONObject jsonObject = new JSONObject(json);
                            String functionName = "";
                            String callback = "";
                            String parameter = "";
                            if (jsonObject.has("functionName")) {
                                functionName = jsonObject.optString("functionName");
                            }
                            callback = jsonObject.optString("callback");
                            parameter = jsonObject.optString("parameter");


                            if (functionName.equals("getDeviceToken")) {
                                String js = "javascript:" + callback + "('" + 89757 + "')";
                                Log.e("eeeeee", "taiYYNative: " + js);
                                webview.loadUrl(js);

                            }
                            if (functionName.equals("getPosition")) {
                                String locationStr = "116.2693206,40.0983397";
                                Log.e("eeeeee", "taiYYNative: " + locationStr);
                                String js = "javascript:receivePosition('" + locationStr + "')";
                                webview.loadUrl(js);
                            }

                            if (functionName.equals("getUserInfo")) {
                                UserInfo userInfo = new UserInfo();
                                userInfo.setName("zhangsan");
                                userInfo.setAge(18);
                                Gson gson = new Gson();
                                String s = gson.toJson(userInfo);

//                                也可以通过JSONObject组装键值对
//                                JSONObject jsonObject1 = new JSONObject();
//                                jsonObject1.put("name", "zhangsan");
//                                jsonObject1.put("age", 19);
//                                String js = "javascript:" + callback + "('" + jsonObject1.toString() + "')";

                                String js = "javascript:" + callback + "('" + s + "')";
                                Log.e("eeeeee", "taiYYNative: " + js);
                                webview.loadUrl(js);
                            }
                        } catch (Exception e) {

                        }
                    }
                }
            });
        }
    }
}